package com.hehe.library.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hehe.library.MyApplication;
import com.hehe.library.MyBook;
import com.hehe.library.R;

import org.w3c.dom.Text;

import io.realm.Realm;

/**
 * Created by xiaoz on 2016/6/15.
 */
public class BookContent extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookcontent);
        initView();
        loadData();
    }

String name;
    String intro;
    String author;
    String num;
    String id;
    MyBook mybook;
    String phone;
    UserInfoEntity userInfoEntity;
    Realm realm;
    private void loadData() {
        String bookid = getIntent().getStringExtra("bookid");
        realm = Realm.getDefaultInstance();
        mybook = realm.where(MyBook.class).equalTo("bookid",bookid).findFirst();
        name = mybook.getBookname();
        intro = mybook.getBookintro();
        author = mybook.getBookauthor();
        num = mybook.getBooknum();
        id = mybook.getBookid();
        mNameTextView.setText(name);
        mIntroTextView.setText(intro);
        mAuthorTextView.setText(author);
        mNumTextView.setText(num);

        phone = MyApplication.getUserInfo().getPhone();
        userInfoEntity = realm.where(UserInfoEntity.class).equalTo("phone",phone).findFirst();
    }

    TextView mNameTextView;
    TextView mIntroTextView;
    TextView mAuthorTextView;
    TextView mNumTextView;
    Button mReturnButton;
    Button mBorrowButton;
    private void initView() {
        mIntroTextView = (TextView) findViewById(R.id.intro_content);
        mNameTextView = (TextView) findViewById(R.id.name_content);
        mAuthorTextView = (TextView) findViewById(R.id.author_content);
        mNumTextView = (TextView) findViewById(R.id.num_content);
        mReturnButton = (Button) findViewById(R.id.return_content);
        mReturnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                realm.beginTransaction();
                userInfoEntity.getBooklist().remove(mybook);
                realm.commitTransaction();
            }
        });
        mBorrowButton = (Button) findViewById(R.id.borrow_content);
        mBorrowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                realm.beginTransaction();
                userInfoEntity.getBooklist().add(mybook);
                realm.commitTransaction();

            }
        });
    }

}
